package org.gruposp2p.proyectokarma.gwt.client.widget;

import org.gruposp2p.proyectokarma.gwt.client.MainEntryPoint;
import org.gruposp2p.proyectokarma.gwt.client.domain.Document;
import org.gruposp2p.proyectokarma.gwt.client.ui.ErrorDialog;
import org.gruposp2p.proyectokarma.gwt.client.ui.document.EditorPanel;
import org.gruposp2p.proyectokarma.gwt.client.util.RequestHelper;
import org.gruposp2p.proyectokarma.gwt.client.util.ServerPaths;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.PopupPanel;

public class DocumentTableRowPopup extends PopupPanel {
	
    MenuBar popupMenuBar = new MenuBar(true);
    MenuItem editDocumentItem = 
    	new MenuItem(MainEntryPoint.INSTANCE.constants.editDocument(), 
    			true, new EditDocumentCommand());
    MenuItem deleteDocumentItem = new MenuItem(
    		MainEntryPoint.INSTANCE.constants.deleteDocument(), true, new DeleteDocumentCommand());
    
    Document selectedDocument;

	public DocumentTableRowPopup() {
		super(true);
        popupMenuBar.addItem(editDocumentItem);
        popupMenuBar.addItem(deleteDocumentItem);
        popupMenuBar.setStyleName("tableRowPopup");
        editDocumentItem.setStyleName("editDocumentItem");
        deleteDocumentItem.setStyleName("deleteDocumentItem");
        add(popupMenuBar);
	}
	
	public void show(Document document) {
		this.selectedDocument = document;
		show();
	}
    
    private class EditDocumentCommand implements Command {
        public EditDocumentCommand() {}
        @Override
        public void execute() {
        	//EditorDialog.getInstance().show(selectedDocument, 
        	//		EditorDialog.Action.EDIT);
        	hide();
        }
    }
    
    private class DeleteDocumentCommand implements Command {
        public DeleteDocumentCommand() {}
        @Override
        public void execute() {
        	hide();
			if (Window.confirm(MainEntryPoint.INSTANCE.constants.confirmDelete(
					selectedDocument.getName()))) {
		    	RequestHelper.doDelete(ServerPaths.getDocumentPath(selectedDocument.getId()), 
		    			new DeleteCallback());
			}
        }
    }
    
    private class DeleteCallback implements RequestCallback {

        @Override
    	public void onError(Request request, Throwable exception) {
        	new ErrorDialog().show("Exception", exception.getMessage());
        	Log.error(exception.getMessage());
    	}

        @Override
    	public void onResponseReceived(Request request, Response response) {
			Log.debug("StatusCode: " + response.getStatusCode() + 
					", Response Text: " + response.getText()); 
    		if (response.getStatusCode() == Response.SC_OK) { } 
    		else if (response.getStatusCode() == Response.SC_UNAUTHORIZED) { }
    		else {
            	new ErrorDialog().show("Error", "Response status: " 
            			+ response.getStatusCode());
    		}
    	}

    }
   
}
